package io;

import http.HttpUtil;
import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: ycy
 * @CreateDate: 2019/10/24 14:32
 */
public class HttpThreadPool {


  public static void main(String[] args) {
    AtomicInteger fair = new AtomicInteger(0);
    AtomicInteger success = new AtomicInteger(0);
    Long startTime;
    AtomicInteger rescount = new AtomicInteger(0);
    AtomicInteger flage = new AtomicInteger(0);  //完成标志
    ExecutorService executorService = Executors.newFixedThreadPool(30);

    try {
      List<String> urls = FileUtils.readLines(new File("D:\\Users\\yincy\\Desktop\\1\\print.txt"), Charset.forName("UTF-8"));
      flage.getAndSet(urls.size());
      System.out.println("请求总数:" + flage.get());
      startTime = System.currentTimeMillis();
      for (String url : urls) {
        Runnable callable = new Runnable() {
          @Override
          public void run() {
            try {
              String s = sendGet("http://10.2.25.194:8080" + url);
            } catch (IOException e) {
              e.printStackTrace();
              fair.incrementAndGet();
              return;
            } finally {
              rescount.incrementAndGet();
            }
            success.incrementAndGet();
          }
        };
        executorService.submit(callable);
      }

      Thread thread2 = new Thread() {
        @Override
        public void run() {
          while (true) {
            try {
              Thread.sleep(2000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            if (flage.get() == rescount.get()) {
              System.out.println("完成所有请求!用时: " + (System.currentTimeMillis() - startTime) + " 成功:" + success.get() + " 失败:" + fair.get());
              return;
            } else {
              System.out.println("已经请求 : " + rescount.get() + "已经耗时:" + (System.currentTimeMillis() - startTime) + " 成功:" + success.get() + " 失败:" + fair.get());
            }
          }
        }
      };

      thread2.setDaemon(true);
      thread2.start();
    } catch (IOException e) {
      e.printStackTrace();
    }


  }



  public static String sendGet(String url) throws MalformedURLException, IOException {
    StringBuffer sb = new StringBuffer("");
    BufferedReader br = null;

    try {
      URLConnection conn = new URL(url).openConnection();

      br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String s = null;
      while ((s = br.readLine()) != null) {
        sb.append(s);
      }
      br.close();
    } finally {
      if (br != null) {
        br.close();
      }
    }
    return sb.toString();
  }

}
